﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class CDivisionController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /CDivision/

        public ActionResult Index()
        {
            return View(db.CDivisions.Where(x => x.RecordStatus == "U").ToList());
        }

        //
        // GET: /CDivision/Details/5

        public ActionResult Details(int id = 0)
        {
            CDivision cdivision = db.CDivisions.Find(id);
            if (cdivision == null)
            {
                return HttpNotFound();
            }
            return View(cdivision);
        }

        //
        // GET: /CDivision/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /CDivision/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CDivision cdivision)
        {
            if (ModelState.IsValid)
            {
                cdivision.RecordStatus = "U";
                cdivision.CreateBy = Session["LogedId"].ToString();
                cdivision.CreateWhen = DateTime.Now;
                db.CDivisions.Add(cdivision);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(cdivision);
        }

        //
        // GET: /CDivision/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CDivision cdivision = db.CDivisions.Find(id);
            if (cdivision == null)
            {
                return HttpNotFound();
            }
            return View(cdivision);
        }

        //
        // POST: /CDivision/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CDivision cdivision)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cdivision).State = EntityState.Modified;
                cdivision.UpdateBy = Session["LogedId"].ToString();
                cdivision.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(cdivision);
        }

        //
        // GET: /CDivision/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CDivision cdivision = db.CDivisions.Find(id);
            if (cdivision == null)
            {
                return HttpNotFound();
            }
            return View(cdivision);
        }

        //
        // POST: /CDivision/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CDivision cdivision = db.CDivisions.Find(id);
            db.Entry(cdivision).State = EntityState.Modified;
            cdivision.RecordStatus = "D";
            cdivision.UpdateBy = Session["LogedId"].ToString();
            cdivision.UpdateWhen = DateTime.Now;
            //db.CDivisions.Remove(cdivision);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}